package com.csu.cphone.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.csu.common.exception.WebException;
import com.csu.common.utils.PasswdUtil;
import com.csu.cphone.constants.ErrorCode;
import com.csu.cphone.dao.CustomerMapper;
import com.csu.cphone.dao.ProducerMapper;
import com.csu.cphone.pojo.CustomerPojo;
import com.csu.cphone.pojo.ProducerPojo;
import com.csu.cphone.service.UserService;

/**
 * @项目名称：cphone
 * @类名称：UserServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月11日 下午5:43:46
 * @company:
 */
@Service
public class UserServiceImpl implements UserService
{

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ProducerMapper producerMapper;

    /**
     * @see com.csu.cphone.service.UserService#customerSign(java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public CustomerPojo customerSign(String wxAppid, String number, String password, String name)
    {
        String passwordMd5 = PasswdUtil.md5Passwd(password);
        CustomerPojo customer = new CustomerPojo();
        customer.setName(name);
        customer.setNumber(number);
        customer.setPasswd(passwordMd5);
        customer.setWxAppid(wxAppid);
        customerMapper.insert(customer);
        return customer;
    }

    /**
     * @throws WebException
     * @see com.csu.cphone.service.UserService#customerLogin(java.lang.String,
     *      java.lang.String, java.lang.String)
     */
    @Override
    public CustomerPojo customerLogin(String wxAppid, String number, String password) throws WebException
    {
        CustomerPojo customer;
        if (number == null || StringUtils.isEmpty(number))
        {
            customer = customerMapper.selectByWxAppid(wxAppid);
        }
        else
        {
            customer = customerMapper.selectByNumber(number);
        }

        if (customer == null)
        {
            throw new WebException(ErrorCode.UserNullCode, ErrorCode.UserNullMessgae);
        }
        if (!PasswdUtil.checkPasswd(password, customer.getPasswd()))
        {
            throw new WebException(ErrorCode.PasswdErrorCode, ErrorCode.PasswdErrorMessgae);
        }

        return customer;
    }

    /**
     * @throws WebException
     * @see com.csu.cphone.service.UserService#producerLogin(java.lang.String,
     *      java.lang.String)
     */
    @Override
    public ProducerPojo producerLogin(String number, String password) throws WebException
    {
        ProducerPojo producer = producerMapper.selectByNumber(number);

        if (producer == null)
        {
            throw new WebException(ErrorCode.UserNullCode, ErrorCode.UserNullMessgae);
        }
        if (!PasswdUtil.checkPasswd(password, producer.getPasswd()))
        {
            throw new WebException(ErrorCode.PasswdErrorCode, ErrorCode.PasswdErrorMessgae);
        }

        return producer;
    }

}
